package com.zu.commu.context;

import com.zu.commu.exception.CommuException;
import com.zu.commu.interf.gprs.Engine;
import com.zu.commu.interf.meter.Driver;
import com.zu.commu.response.ResponseFactoryImp;
import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

public class ParseGPRSConf {
	private Document doc;

	public void createDom() throws CommuException {
		URL configFileURL = ParseGPRSConf.class.getResource("/GPRS.xml");
		if (configFileURL == null) {
			configFileURL = ParseGPRSConf.class.getResource("/gprs.xml");
		}
		if (configFileURL == null)
			throw new CommuException().setMessage("没有得到GPRS.xml配置!", null);
		try {
			SAXBuilder sb = new SAXBuilder();
			this.doc = sb.build(configFileURL);
			if (this.doc == null)
				throw new CommuException().setMessage("没有生成GPRS.xml配置的DOM对象!", null);
		} catch (Exception e) {
			throw new CommuException().setMessage("没有得到GPRS.xml配置!", e);
		}
	}

	public void parseGPRSProtocolConf() throws CommuException {
		Element root = this.doc.getRootElement();
		if (root == null) {
			throw new CommuException().setMessage("没有得到GPRS.xml配置根元素configs!", null);
		}
		Element protocols = root.getChild("gprsProtocols");
		if (protocols == null) {
			throw new CommuException().setMessage("GPRS.xml中元素gprsProtocols配置不正确!", null);
		}
		List protocolList = protocols.getChildren();
		if ((protocolList == null) || (protocolList.size() == 0)) {
			throw new CommuException().setMessage("GPRS.xml中元素gprsProtocols配置不正确!", null);
		}

		int num = protocolList.size();
		ServerContext.GPRSProtocolNames = new String[num];
		ServerContext.GPRSProtocol_DriverNames = new Hashtable();
		ServerContext.GPRSProtocol_sendHeartBeat = new Hashtable();
		ServerContext.GPRSProtocol_heartBeatString = new Hashtable();
		ServerContext.GPRSProtocol_heartBeatTime = new Hashtable();
		ServerContext.GPRSProtocol_returnHeartBeat = new Hashtable();

		num = 0;

		Iterator it = protocolList.iterator();
		Element tempe = null;
		String name = null;
		String driverClass = null;
		String sendHeartBeat = null;
		String heartBeatString = null;
		String heartBeatTime = null;
		String returnHeartBeat = null;
		while (it.hasNext()) {
			Element e = (Element) it.next();
			tempe = e.getChild("name");
			name = tempe.getText();
			if ((tempe == null) || (name == null) || (name.trim().equals(""))) {
				throw new CommuException().setMessage("GPRS.xml中元素protocols.gprsProtocols.name配置不正确!", null);
			}
			for (int i = 0; i < ServerContext.GPRSProtocolNames.length; i++) {
				if ((ServerContext.GPRSProtocolNames[i] != null) && (ServerContext.GPRSProtocolNames[i].equals(name))) {
					throw new CommuException().setMessage("GPRS.xml中元素protocols.gprsProtocols.name名称：" + name + "重复应用!", null);
				}
			}
			ServerContext.GPRSProtocolNames[(num++)] = name.trim();

			tempe = e.getChild("driverClass");
			driverClass = tempe.getText();
			if ((tempe == null) || (driverClass == null) || (driverClass.trim().equals(""))) {
				throw new CommuException().setMessage("GPRS.xml中元素protocols.gprsProtocols.driverClass配置不正确!", null);
			}
			if (ServerContext.GPRSProtocol_DriverNames.containsValue(driverClass.trim())) {
				throw new CommuException().setMessage("GPRS.xml中元素protocols.gprsProtocols.driverClass名称：" + driverClass + "重复应用!", null);
			}
			ServerContext.GPRSProtocol_DriverNames.put(name, driverClass.trim());

			tempe = e.getChild("sendHeartBeat");
			sendHeartBeat = tempe.getText();
			if ((tempe == null) || (sendHeartBeat == null) || (sendHeartBeat.trim().equals(""))) {
				throw new CommuException().setMessage("GPRS.xml中元素protocols.gprsProtocols.sendHeartBeat配置不正确!", null);
			}
			ServerContext.GPRSProtocol_sendHeartBeat.put(name.trim(), sendHeartBeat.trim());

			tempe = e.getChild("heartBeatString");
			heartBeatString = tempe.getText();
			if ((tempe == null) || (heartBeatString == null)) {
				throw new CommuException().setMessage("GPRS.xml中元素protocols.gprsProtocols.heartBeatString配置不正确!", null);
			}
			ServerContext.GPRSProtocol_heartBeatString.put(name.trim(), heartBeatString.trim());

			tempe = e.getChild("heartBeatTime");
			heartBeatTime = tempe.getText();
			if ((tempe == null) || (heartBeatTime == null) || (heartBeatTime.trim().equals("")))
				throw new CommuException().setMessage("GPRS.xml中元素protocols.gprsProtocols.heartBeatTime配置不正确!", null);
			try {
				Integer.parseInt(heartBeatTime.trim());
			} catch (Exception eint) {
				throw new CommuException().setMessage("GPRS.xml中元素protocols.gprsProtocols.heartBeatTime配置不正确!", null);
			}
			ServerContext.GPRSProtocol_heartBeatTime.put(name.trim(), heartBeatTime.trim());

			tempe = e.getChild("returnHeartBeat");
			returnHeartBeat = tempe.getText();
			if ((tempe == null) || (returnHeartBeat == null) || (returnHeartBeat.trim().equals("")))
				throw new CommuException().setMessage("GPRS.xml中元素protocols.gprsProtocols.returnHeartBeat配置不正确!", null);
			try {
				Integer.parseInt(returnHeartBeat.trim());
			} catch (Exception eint) {
				throw new CommuException().setMessage("GPRS.xml中元素protocols.gprsProtocols.returnHeartBeat配置不正确!", null);
			}
			if ((!returnHeartBeat.equals("1")) && (!returnHeartBeat.equals("0")) && (!returnHeartBeat.equals("9"))) {
				throw new CommuException().setMessage("GPRS.xml中元素protocols.gprsProtocols.returnHeartBeat配置不正确!", null);
			}
			ServerContext.GPRSProtocol_returnHeartBeat.put(name.trim(), returnHeartBeat.trim());
		}
	}

	public void parseMeterProtocolConf() throws CommuException {
		Element root = this.doc.getRootElement();
		if (root == null) {
			throw new CommuException().setMessage("没有得到GPRS.xml配置根元素configs!", null);
		}
		Element protocols = root.getChild("meterprotocols");
		if (protocols == null) {
			throw new CommuException().setMessage("GPRS.xml中元素meterprotocols配置不正确!", null);
		}
		List protocolList = protocols.getChildren();
		if ((protocolList == null) || (protocolList.size() == 0)) {
			throw new CommuException().setMessage("GPRS.xml中元素meterprotocols配置不正确!", null);
		}

		int num = protocolList.size();
		ServerContext.meterProtocolNames = new String[num];
		ServerContext.meterProtocol_DriverNames = new Hashtable();
		ServerContext.meterProtocol_sendOrderTimes = new Hashtable();
		ServerContext.meterProtocol_sendOrderWaitTime = new Hashtable();
		num = 0;

		Iterator it = protocolList.iterator();
		Element tempe = null;
		String name = null;
		String driverClass = null;
		String sendOrderTimes = null;
		String sendOrderWaitTime = null;
		while (it.hasNext()) {
			Element e = (Element) it.next();
			tempe = e.getChild("name");
			name = tempe.getText();
			if ((tempe == null) || (name == null) || (name.trim().equals(""))) {
				throw new CommuException().setMessage("GPRS.xml中元素protocols.meterprotocols.name配置不正确!", null);
			}
			for (int i = 0; i < ServerContext.meterProtocolNames.length; i++) {
				if ((ServerContext.meterProtocolNames[i] != null) && (ServerContext.meterProtocolNames[i].equals(name.trim()))) {
					throw new CommuException().setMessage("GPRS.xml中元素protocols.meterprotocols.name名称：" + name + "重复应用!", null);
				}
			}
			ServerContext.meterProtocolNames[(num++)] = name.trim();

			tempe = e.getChild("driverClass");
			driverClass = tempe.getText();
			if ((tempe == null) || (driverClass == null) || (driverClass.trim().equals(""))) {
				throw new CommuException().setMessage("GPRS.xml中元素protocols.meterprotocols.driverClass配置不正确!", null);
			}
			if (ServerContext.meterProtocol_DriverNames.containsValue(driverClass.trim())) {
				throw new CommuException().setMessage("GPRS.xml中元素protocols.meterprotocols.driverClass名称：" + driverClass + "重复应用!", null);
			}
			ServerContext.meterProtocol_DriverNames.put(name.trim(), driverClass.trim());

			tempe = e.getChild("sendOrderTimes");
			sendOrderTimes = tempe.getText();
			if ((tempe == null) || (sendOrderTimes == null) || (sendOrderTimes.trim().equals("")))
				throw new CommuException().setMessage("GPRS.xml中元素sendOrderTimes配置不正确!", null);
			try {
				Integer.parseInt(sendOrderTimes.trim());
			} catch (Exception eint) {
				throw new CommuException().setMessage("GPRS.xml中元素sendOrderTimes配置不正确!", eint);
			}
			ServerContext.meterProtocol_sendOrderTimes.put(name.trim(), sendOrderTimes.trim());

			tempe = e.getChild("sendOrderWaitTime");
			sendOrderWaitTime = tempe.getText();
			if ((tempe == null) || (sendOrderWaitTime == null) || (sendOrderWaitTime.trim().equals("")))
				throw new CommuException().setMessage("GPRS.xml中元素sendOrderWaitTime配置不正确!", null);
			try {
				Float.parseFloat(sendOrderWaitTime.trim());
			} catch (Exception eint) {
				throw new CommuException().setMessage("GPRS.xml中元素sendOrderWaitTime配置不正确!", eint);
			}
			ServerContext.meterProtocol_sendOrderWaitTime.put(name.trim(), sendOrderWaitTime.trim());
		}
	}

	public void parseGPRSConf() throws CommuException {
		Element root = this.doc.getRootElement();
		if (root == null) {
			throw new CommuException().setMessage("没有得到GPRS.xml配置根元素configs!", null);
		}

		Element gprss = root.getChild("gprses");
		if (gprss == null) {
			throw new CommuException().setMessage("没有得到GPRS.xml配置根元素gprses!", null);
		}

		List gprses = gprss.getChildren();
		if ((gprses == null) || (gprses.size() == 0)) {
			Logger log = Logger.getLogger("ParseGPRSConf");
			log.warn("没有一个GPRS配置！");
			return;
		}

		ServerContext.GPRSID_GPRSProtocol = new Hashtable();
		ServerContext.GPRSID_meterProtocol = new Hashtable();
		ServerContext.GPRSProtocol_GPRSID = new Hashtable();
		ServerContext.meterProtocol_GPRSID = new Hashtable();
		for (int i = 0; i < ServerContext.GPRSProtocolNames.length; i++) {
			ServerContext.GPRSProtocol_GPRSID.put(ServerContext.GPRSProtocolNames[i], new ArrayList());
		}
		for (int i = 0; i < ServerContext.meterProtocolNames.length; i++) {
			ServerContext.meterProtocol_GPRSID.put(ServerContext.meterProtocolNames[i], new ArrayList());
		}

		Iterator it = gprses.iterator();
		Element tempe = null;
		String id = null;
		String protocol = null;
		while (it.hasNext()) {
			Element e = (Element) it.next();
			tempe = e.getChild("id");
			id = tempe.getText();
			if ((tempe == null) || (id == null) || (id.trim().equals(""))) {
				throw new CommuException().setMessage("GPRS.xml中元素protocols.gprs.id配置不正确!", null);
			}
			if (ServerContext.GPRSID_GPRSProtocol.containsKey(id.trim())) {
				throw new CommuException().setMessage("GPRS.xml中元素protocols.gprs.id=" + id + "重复配置!", null);
			}

			tempe = e.getChild("gprsProtocol");
			protocol = tempe.getText();
			if ((tempe == null) || (protocol == null) || (protocol.trim().equals(""))) {
				throw new CommuException().setMessage("GPRS.xml中元素protocols.gprs.gprsProtocol配置不正确!", null);
			}

			if (!ServerContext.GPRSProtocol_DriverNames.containsKey(protocol)) {
				Logger log = Logger.getLogger("ParseGPRSConf");
				log.error("GPRS.xml中元素protocols.gprs.id=" + id + "对应的GPRS通信协议" + protocol + "不存在!");
			}
			setGPRSID_GPRSProtocol(id.trim(), protocol.trim());

			tempe = e.getChild("meterProtocol");
			protocol = tempe.getText();
			if ((tempe == null) || (protocol == null) || (protocol.trim().equals(""))) {
				throw new CommuException().setMessage("GPRS.xml中元素protocols.gprs.meterProtocol配置不正确!", null);
			}
			if (!ServerContext.meterProtocol_DriverNames.containsKey(protocol.trim())) {
				Logger log = Logger.getLogger("ParseGPRSConf");
				log.error("GPRS.xml中元素protocols.gprs.id=" + id + "对应的测控器通信协议" + protocol + "不存在!");
			}
			setGPRSID_meterProtocol(id.trim(), protocol.trim());
		}
	}

	private void setGPRSID_GPRSProtocol(String id, String protocol) {
		ServerContext.GPRSID_GPRSProtocol.put(id, protocol);
		ArrayList list = (ArrayList) ServerContext.GPRSProtocol_GPRSID.get(protocol);
		list.add(id);
	}

	private void setGPRSID_meterProtocol(String id, String protocol) {
		ServerContext.GPRSID_meterProtocol.put(id, protocol);
		ArrayList list = (ArrayList) ServerContext.meterProtocol_GPRSID.get(protocol);
		list.add(id);
	}

	public void createGPRSProtocolDriver() throws CommuException {
		ServerContext.GPRSProtocol_Drivers = new Hashtable();
		String protocol = null;
		String driverClass = null;
		for (int i = 0; i < ServerContext.GPRSProtocolNames.length; i++) {
			Engine engine = null;
			protocol = ServerContext.GPRSProtocolNames[i];
			driverClass = (String) ServerContext.GPRSProtocol_DriverNames.get(protocol);
			try {
				Class c = Class.forName(driverClass);
				if (c != null) {
					Object o = c.newInstance();
					if (o != null) {
						engine = (Engine) o;
						engine.setOnlyDriver();
						engine.init((ArrayList) ServerContext.GPRSProtocol_GPRSID.get(protocol), ResponseFactoryImp.newInstance());
					}
				}
			} catch (Exception e) {
				throw new CommuException().setMessage("初始化GPRS协议分析器" + driverClass + "失败!", e);
			}
			if (engine != null)
				ServerContext.GPRSProtocol_Drivers.put(protocol, engine);
		}
	}

	public void createMeterProtocolDriver() throws CommuException {
		ServerContext.meterProtocol_Drivers = new Hashtable();
		String protocol = null;
		String driverClass = null;
		for (int i = 0; i < ServerContext.meterProtocolNames.length; i++) {
			Driver driver = null;
			protocol = ServerContext.meterProtocolNames[i];
			driverClass = (String) ServerContext.meterProtocol_DriverNames.get(protocol);
			try {
				Class c = Class.forName(driverClass);
				if (c != null) {
					Object o = c.newInstance();
					if (o != null) {
						driver = (Driver) o;
						driver.setOnlyDriver();
						driver.init((ArrayList) ServerContext.meterProtocol_GPRSID.get(protocol));
					}
				}
			} catch (Exception e) {
				throw new CommuException().setMessage("初始化GPRS协议分析器" + driverClass + "失败!", e);
			}
			if (driver != null)
				ServerContext.meterProtocol_Drivers.put(protocol, driver);
		}
	}
}